/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUISeletorAmigoConversaGrupo.java
 *
 * Created on 09/12/2010, 04:28:29
 */
package ifpb.pod.cliente.visao;

import ifpb.pod.cliente.funcionalidades.FachadaCliente;
import ifpb.pod.model.ContaUsuario;
import ifpb.pod.model.Contato;
import ifpb.pod.model.Usuario;
import ifpb.pod.remote.ClienteRemote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author Diogenes
 */
public class GUISeletorAmigoConversaGrupo extends javax.swing.JFrame {

//    private List<Contato> contatosOn;
    private Contato remetente;
    private DefaultListModel model;
    private FachadaCliente fachada;
    private List<Contato> contatosSelecionar;
    private List<ClienteRemote> clientesNaConversa;

    /** Creates new form GUISeletorAmigoConversaGrupo */
    public GUISeletorAmigoConversaGrupo(Contato remetente, List<Contato> contatosSelecionar, List<ClienteRemote> clientesNaConversa) {
        initComponents();
        this.remetente = remetente;
        this.contatosSelecionar = contatosSelecionar;
        this.clientesNaConversa = clientesNaConversa;
        fachada = FachadaCliente.getInstance();
//        inicializa();
        configuraLista();
    }

    private void inicializa() {
//        ContaUsuario uc = new ContaUsuario();
//        uc.setLogin(remetente.getLogin());
//        Usuario u = fachada.buscarUsuario(uc);
//        contatosOn = fachada.getAmigosOnline(u);
    }

    private void configuraLista() {
        model = new DefaultListModel();
        lContatos.setModel(model);
        for (Contato contato : contatosSelecionar) {
            model.addElement(contato);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        lContatos = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lContatos.setBorder(javax.swing.BorderFactory.createTitledBorder("Contatos"));
        lContatos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lContatos.setOpaque(false);
        lContatos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lContatosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lContatos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lContatosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lContatosMouseClicked
        try {
            if (evt.getClickCount() == 2) {
                FachadaCliente fachada = FachadaCliente.getInstance();
                Contato destinatario = (Contato) model.getElementAt(lContatos.getSelectedIndex());
                try {
                    //                fachada.conversar(remetente, destinatario);
                  fachada.solicitarConversaGrupo(remetente, destinatario, toContatos(clientesNaConversa), true);
                  this.dispose();
                } catch (RemoteException ex) {
                    ex.printStackTrace();
                }

            }
        } catch (ArrayIndexOutOfBoundsException ex) {
            System.out.println("fora dos limites");
        }

    }//GEN-LAST:event_lContatosMouseClicked

    private List<Contato> toContatos(List<ClienteRemote> clientes){
        List<Contato> contatos = new ArrayList<Contato>();
        for (ClienteRemote cliente : clientes) {
            try {
                contatos.add(cliente.getContato());
            } catch (RemoteException ex) {
                ex.printStackTrace();
            }
        }
        return contatos;
        
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lContatos;
    // End of variables declaration//GEN-END:variables
}
